package com.android.common.base;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;

public class BaseFragmentAdapter extends FragmentStateAdapter {
    private List<? extends Fragment> fragments = new ArrayList<>();

    public BaseFragmentAdapter(@NonNull FragmentActivity fragmentActivity, List<? extends Fragment> fragments) {
        super(fragmentActivity);
        this.fragments = fragments;
    }

    public BaseFragmentAdapter(@NonNull Fragment fragment, List<? extends Fragment> fragments) {
        super(fragment);
        this.fragments = fragments;
    }

    public BaseFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }


    /**
     * <a href="https://cloud.tencent.com/developer/ask/sof/757005">...</a>
     */
    @Override
    public long getItemId(int position) {
        return fragments.get(position).hashCode();
    }
}
